const vm = new Vue({
    el: '#main',
    data: {
        imgUrl: '../image/id_card/id-card-e.jpg',
        imgFile: null,
    },
});

const vmFooter = new Vue({
    el: 'div.footer',
    data: {
        uploadedImg: false,
    },
});

function apiready() {
    main();
}

function main() {
    fxj.fixLayout();
    listenFile();
}

function uploadIdCard() {
    document.getElementById('idCard').click();
}

function listenFile() {
    const fileContainer = document.getElementById('idCard');
    fileContainer.addEventListener('change', async e => {
        const file = e.target.files.length > 0 ? e.target.files[0] : null;
        if (file) {
            vm.imgFile = file;
            const formData = new FormData();
            formData.append('imgFile', file);
            formData.append('token', localStorage.getItem('token'));
            formData.append('order', 2);
            uploadImg(formData);
            vm.imgUrl = await fxj.readFile(file);
        }
    });
}

async function uploadImg(formData) {
    const close = fxj.dialog.loading({
        des: '正在上传图片,请稍后',
    });
    let res = await fetch(`${fxj.serverAddress}home/appUploadImg/uploadIdCardImg`, {
        method: 'POST',
        body: formData,
    });
    res = await res.json();
    close();
    toast(res.msg);
    if (res.err === 0) {
        vmFooter.uploadedImg = true;
    }
}

function ok() {
    if (vmFooter.uploadedImg) {
        closeToWin();
        api.execScript({
            script: 'reqMeInfo()',
            name: 'root',
            frameName: 'me',
        });
    }
    else {
        toast('请上传身份证');
    }
}